/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.service.knowledge;

import cn.ac.nercis.pes.model.dto.common.ItemData;
import cn.ac.nercis.pes.model.dto.common.Result;
import cn.ac.nercis.pes.model.dto.knowledge.KmRiskAnalysisConfigDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 风险分析信息（知识库）服务
 *
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
@Service
public record KmRiskAnalysisService(KmVulnerabilityDescService kmVulnerabilityDescService,
                                    KmRiskDescService kmRiskDescService,
                                    KmRiskDefinitionService kmRiskDefinitionService,
                                    KmSuggestService kmSuggestService) {
    @Autowired
    public KmRiskAnalysisService {
    }

    public Result<KmRiskAnalysisConfigDTO> getKmRiskAnalysisConfig(String unitTargetId){
        var vdTemplates = kmVulnerabilityDescService.findKmVulnerabilityDesc(unitTargetId)
                .stream()
                .map(vd->new ItemData(vd.getDesc(),vd.getId()))
                .toList();
        var riskDescTemplates = kmRiskDescService.findKmRiskDesc(unitTargetId)
                .stream()
                .map(rd->new ItemData(rd.getDesc(),rd.getId()))
                .toList();
        var riskDefinedTemplates = kmRiskDefinitionService.findKmRiskDefinition(unitTargetId)
                .stream()
                .map(rd->new ItemData(rd.getContent(),rd.getId()))
                .toList();
        var suggestTemplates = kmSuggestService.findKmSuggest(unitTargetId)
                .stream()
                .map(s->new ItemData(s.getSuggest(),s.getId()))
                .toList();
        return Result.success(KmRiskAnalysisConfigDTO.builder()
                .vulnerabilityDescTemplates(vdTemplates)
                .riskDescTemplates(riskDescTemplates)
                .riskDefinitionTemplates(riskDefinedTemplates)
                .suggestTemplates(suggestTemplates)
                .build());
    }

}